package com.deep.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器
 *
 * @author Deep
 * @date 2022/1/17
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set;

    // 校验器初始化
    @Override
    public void initialize(ListValue constraintAnnotation) {
        set = new HashSet<>();

        for (int value : constraintAnnotation.values()) {
            set.add(value);
        }
    }

    // 校验器进行校验
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
